<?php

$periods = array();

for ($i=2013; $i<date("Y")+2; $i++){
    for ($j=1; $j<13; $j++){
        $periods[] = str_pad($j, 2, "0", STR_PAD_LEFT) . '/' . $i;
    }
}

$GLOBALS['template_data']['periods'] = $periods;
$GLOBALS['template_data']['today'] = date("m/Y");
$GLOBALS['template_data']['show'] = 0;

if (isset($_REQUEST['period']) && !empty ($_REQUEST['period'])){
    $GLOBALS['template_data']['show'] = 1;
    $selectedPeriod = $_REQUEST['period'];
    $selectedPeriodExplode = explode('/', $selectedPeriod);
    $GLOBALS['template_data']['selectedMonthName'] = date("F", mktime(12, 0, 0, $selectedPeriodExplode[0], 2, $selectedPeriodExplode[1]));
    $GLOBALS['template_data']['selectedMonth'] = $selectedPeriodExplode[0];
    $GLOBALS['template_data']['selectedYear'] = $selectedPeriodExplode[1];
    
    $filter = array(
        'client' => 11,
        'userlevel' => USER_TYPE_TECHNIC,
        'active' => 1
    );
    
    $db_obj = new db_obj();
    $user_obj = new user_obj();
    $record_obj = new record_obj();
    $tariff_obj = new tariff_obj();
    $users = $user_obj->getUsers('users_id, name, surname', $filter);
    if (error_obj::isError($users)){
        error_obj::setError($user_data, UNEXCEPTED);
    } else {
        
        foreach ($users as $key => $value) {
            $sql = "SELECT SUM(monthlyFee) as suma FROM ".$GLOBALS['tb_clients']." WHERE contactPerson = '".$value['users_id']."' AND active='0'";
            $users[$key]['second'] = round($db_obj->getOne($sql),2);
            if (empty($users[$key]['second'])){
                $users[$key]['second'] = 0;
            }
            
            $filter = array(
                'user_id' => $value['users_id'],
                'monthm' => $selectedPeriodExplode[0],
                'year' => $selectedPeriodExplode[1],
                'nottariff' => -1
            );
            $records = $record_obj->getListOfRecords($filter);
            $servis_sum = 0;
            foreach ($records as $k => $record) {
                $tariff = $tariff_obj->getTariffById($record['tariff']);
                $servis_sum += round(($record['amount'] * $tariff['servis_tariff']),2);
            }
            $users[$key]['first'] = $servis_sum;
            
        }
        
        $GLOBALS['template_data']['users'] = $users;
    }
    
}

?>
